//
//  OderDetailTFView.m
//  Rabbit
//
//  Created 阿帕 on 2019/12/17.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "OderDetailTFView.h"
#import "CJTextField.h"
#import "HomeXieYView.h"
@interface OderDetailTFView ()
{
    
    NSString *_dateString;
    /**
     0 非报价单 货源单 独立司机
     1 非报价单 非货源单 独立司机
     2 报价单 货源单 独立司机
     3 报价单 非货源单 独立司机
     4 非报价单 货源单 车队
     5 非报价单 非货源单 车队
     6 报价单 货源单 车队
     7 报价单 非货源单 车队
     */
    NSInteger _orderState;//订单类型
    
    BOOL _isAgree;
}
@property(nonatomic, strong)HomeXieYView        *xieyiView;
@property(nonatomic, assign)NSInteger btnTage;
@end

@implementation OderDetailTFView
static const NSInteger tf_Height = 40;

#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    
    _dateString = @"";
    _orderState = 0;
    _isAgree = NO;
    
    NSArray *titles = @[@"承运数量",@"承运车数",@"报价单价(元/吨)",@"到达时间"];
    NSArray *placeholders = @[@"请填写每车承运数量",@"请填写车数",@"请填写价格"];
    
    //输入框 和 时间选择
    for (int i=0; i<4; i++) {
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, tf_Height*i, ScreenW, tf_Height)];
        bgView.tag = 10 + i;
        bgView.hidden = YES;
        bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bgView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 140, tf_Height)];
        label.textColor = Dark_TextColor;
        label.text = titles[i];
        label.font = font(14);
        if (i==0) {
            label.tag = 666;
        } else if (i==2) {
            label.tag = 888;
        }
        label.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:label];
        
        if (i != 3) {
            //输入框
            CJTextField*textField = [[CJTextField alloc] initWithFrame:CGRectMake(140, 0, ScreenW-140-15, tf_Height)];
            textField.tag = 20 + i;
            textField.font = font(14);
            textField.backgroundColor = [UIColor cy_colorWithHexString:@"#fff"];
            textField.textAlignment = NSTextAlignmentRight;
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholders[i] attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
            [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
//            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            
            [bgView addSubview:textField];
            
            if (i == 0) {
                //承运数量
                textField.textFieldType = CJTextFieldStringTypeThreeDecimalPlaces;
                textField.keyboardType = UIKeyboardTypeDecimalPad;
                textField.maxLength = 20;
            } else if (i == 1) {
                //承运车数
                textField.textFieldType = CJTextFieldStringTypeNumber;
                textField.keyboardType = UIKeyboardTypeNumberPad;
                textField.maxLength = 5;
                
            } else if (i == 2) {
                //报价单价
                textField.textFieldType = CJTextFieldStringTypeMoneyValue;
                textField.keyboardType = UIKeyboardTypeDecimalPad;
                textField.maxLength = 10;
                
//                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenW - 70, 0, 50, tf_Height)];
//                label.text = @"元/吨";
//                label.textColor = Dark_TextColor;
//                label.textAlignment = NSTextAlignmentRight;
//                label.font = font(14);
//                [bgView addSubview:label];
            }
            
        } else {
            //按钮
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = 1000;
            [button setTitleColor:Dark_TextColor forState:UIControlStateNormal];
            [button setTitle:@"请选择时间" forState:UIControlStateNormal];
            button.titleLabel.font = font(14);
            button.frame = CGRectMake(80, 0, ScreenW-95, tf_Height);
            [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [bgView addSubview:button];
        }
        
    }
    
    //接单按钮
    UIButton *jiedanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    jiedanButton.tag = 1001;
    cut(jiedanButton, BTN_BORDER_RADIUS);
    [jiedanButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    jiedanButton.backgroundColor = Theme_ColorValue;
    [jiedanButton setTitle:@"确定" forState:UIControlStateNormal];
    jiedanButton.titleLabel.font = font(16);
    jiedanButton.frame = CGRectMake(20, self.cy_height-50, self.cy_width-40, tf_Height);
    [jiedanButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:jiedanButton];

//    //接单协议
    UIButton *xieYiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    xieYiButton.tag = 1100;
    xieYiButton.titleLabel.font = font(12);
    [xieYiButton setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
    [xieYiButton setTitle:CJStringWithFormat(@"同意《%@平台与承运方货物运输交易协议》", APP_PlatformName) forState:UIControlStateNormal];
    CGSize bSize = [DCSpeedy dc_calculateTextSizeWithText:CJStringWithFormat(@"同意《%@平台与承运方货物运输交易协议》", APP_PlatformName) WithTextFont:12 WithMaxW:1000];
    xieYiButton.frame = CGRectMake(50, self.cy_height-120, bSize.width, 30);
    xieYiButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [xieYiButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:xieYiButton.titleLabel.text];
    [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:Light_TextColor range:[xieYiButton.titleLabel.text rangeOfString:@"同意"]];
    [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:Theme_ColorValue range:[xieYiButton.titleLabel.text rangeOfString:CJStringWithFormat(@"《%@平台与承运方货物运输交易协议》", APP_PlatformName)]];
    [xieYiButton setAttributedTitle:attString forState:UIControlStateNormal];
    [self addSubview:xieYiButton];

    UIButton *agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    agreeButton.tag = 1101;
    [agreeButton setImage:image(@"gcy_img_home_business_nomal") forState:UIControlStateNormal];
    agreeButton.frame = CGRectMake(xieYiButton.left - 30, xieYiButton.top + 5, 20, 20);
    [agreeButton addTarget:self action:@selector(xieYiButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:agreeButton];
}

- (void)xieYiButtonAction:(UIButton *)button {
    button.selected = !button.selected;
    if (button.isSelected) {
        _isAgree = YES;
        [button setImage:[image(@"gcy_img_home_business_select") imageChangeColor:Theme_ColorValue] forState:UIControlStateNormal];
    } else {
        _isAgree = NO;
        [button setImage:image(@"gcy_img_home_business_nomal") forState:UIControlStateNormal];
    }
   
}

- (void)buttonAction:(UIButton *)button {
    
    WEAKSELF
    
    UITextField *shuLiangTextField = [weakSelf viewWithTag:20];
    UITextField *cheShuTextField = [weakSelf viewWithTag:21];
    UITextField *danJiaTextField = [weakSelf viewWithTag:22];
    self.btnTage = button.tag;
    if (button.tag == 1000) {
        
        BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMDHM];
        datePickerView.title = @"选择日期";
        datePickerView.isAutoSelect = NO;
        datePickerView.showToday = NO; // 是否显示“今天”
        datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
            NSLog(@"选择的值：%@", selectValue);
            _dateString = selectValue;
            
            UIButton *button = [weakSelf viewWithTag:1000];
            [button setTitle:selectValue forState:UIControlStateNormal];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(getTextFieldAndDateValue:andCheShu:andDanjia:andDate:)]) {
                [self.delegate getTextFieldAndDateValue:shuLiangTextField.text andCheShu:cheShuTextField.text andDanjia:danJiaTextField.text andDate:selectValue];
            }
        };
        [datePickerView show:nil];
        
    } else if (button.tag == 1001) {
        if (_isAgree == NO) {
            [XSInfoView showInfo:@"请先同意交易协议" onView:self.window];
            return;
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(getTextFieldAndDateValue:andCheShu:andDanjia:andDate:)]) {
            [self.delegate getTextFieldAndDateValue:shuLiangTextField.text andCheShu:cheShuTextField.text andDanjia:danJiaTextField.text andDate:_dateString];
        }
    }
    if([self.orderInfoDic[@"judgeContract"] intValue] == 1 && button.tag != 1100){
        self.xieyiView = [[HomeXieYView alloc] initWithFrame:CGRectMake(0, 0,ScreenW, ScreenH) Url:CJStringWithFormat(@"%@/agreement/agreement.html?agreementType=5", BasicDomainURL)];
        [self.xieyiView.comitButton addTarget:self action:@selector(xiYiBtnClick) forControlEvents:UIControlEventTouchUpInside];
       [ [UIApplication sharedApplication].keyWindow addSubview:self.xieyiView];
    }else{
        [self xiYiBtnClick];
    }
}
- (void)xiYiBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(orderDetailButtonClick:andOrderState:LastWeight:NotReceivedCount:)]) {
        [self.delegate orderDetailButtonClick:self.btnTage andOrderState:_orderState LastWeight:self.orderInfoDic[@"lastWeight"] NotReceivedCount:self.orderInfoDic[@"notReceivedCount"]];
    }
    [self.xieyiView removeFromSuperview];
}

- (void)textFieldChanged:(UITextField *)textField {
//    UITextField *shuLiangTextField = [self viewWithTag:20];
//    UITextField *cheShuTextField = [self viewWithTag:21];
//    UITextField *danJiaTextField = [self viewWithTag:22];
//
//    if (self.delegate && [self.delegate respondsToSelector:@selector(getTextFieldAndDateValue:andCheShu:andDanjia:andDate:)]) {
//        [self.delegate getTextFieldAndDateValue:shuLiangTextField.text andCheShu:cheShuTextField.text andDanjia:danJiaTextField.text andDate:_dateString];
//    }
}

- (void)setOrderInfoDic:(NSDictionary *)orderInfoDic {
    
    UIView *shuLiangView = [self viewWithTag:10];
    UIView *cheShuView = [self viewWithTag:11];
    UIView *danJiaView = [self viewWithTag:12];
    UIView *timeView = [self viewWithTag:13];
    
    UILabel *numTextLabel = [self viewWithTag:666];
    UILabel *priceTextLabel = [self viewWithTag:888];
    
    UIButton *jieDanBtn = [self viewWithTag:1001];
    UIButton *xieYiButton = [self viewWithTag:1100];
    UIButton *agreeButton = [self viewWithTag:1101];
    
    _orderInfoDic = orderInfoDic;
    
    priceTextLabel.text = [NSString stringWithFormat:@"报价单价(%@)", orderInfoDic[@"isTrunkName"]];
    
    if ([orderInfoDic[@"coalUnit"] integerValue] == 0) {
        numTextLabel.text = @"承运数量(吨)";
    } else if ([orderInfoDic[@"coalUnit"] integerValue] == 1) {
        numTextLabel.text = @"承运数量(方)";
    } else if ([orderInfoDic[@"coalUnit"] integerValue] == 2) {
        numTextLabel.text = @"承运数量(件)";
        UITextField *textField = [self viewWithTag:20];
        textField.textFieldType = CJTextFieldStringTypeNumber;
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.maxLength = 10;
    }
    //    报价单 0不允许报价
    NSString *isBaoJiaDan = CJStringWithFormat(@"%@",_orderInfoDic[@"isQuote"]);
    //货源单 0货源单
    NSString *isHuoYuanDan = CJStringWithFormat(@"%@",_orderInfoDic[@"coalNumber"]);
    //0独立司机 1车队 2车队司机
    NSString *driverType = CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]);
    
//        NSString *isBaoJiaDan = @"0";
//        //货源单 0货源单
//        NSString *isHuoYuanDan = @"0";
//        //0独立司机 1车队 2车队司机
//        NSString *driverType = @"2";
    
    //车队司机不能接单 需要车队给其分配
    
    if ([isBaoJiaDan isEqual:@"0"] || [isBaoJiaDan isEqual:@"3"]) {
        if ([isHuoYuanDan isEqual:@"0"]) {
            if ([driverType isEqual:@"0"]) {
                //非报价单 货源单 独立司机
                _orderState = 0;
                
                shuLiangView.hidden = NO;
                
                [jieDanBtn setTitle:@"接单" forState:UIControlStateNormal];
                jieDanBtn.top = shuLiangView.cy_bottom + 10;
                self.cy_height = jieDanBtn.cy_bottom + 45;
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                    [self.delegate calculateSelfViewHeight:jieDanBtn.cy_bottom + 45];
                }
                
            } else {
                
                if ([driverType isEqual:@"2"]) {
                    //车队司机不能接单 隐藏接单区域
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                        [self.delegate calculateSelfViewHeight:0];
                    }
                    
                } else {
                    
                    //非报价单 货源单 车队
                    _orderState = 4;
                    
                    shuLiangView.hidden = NO;
                    cheShuView.hidden = NO;
                    [jieDanBtn setTitle:@"批量接单" forState:UIControlStateNormal];
                    
                    jieDanBtn.top = cheShuView.cy_bottom + 10;
                    self.cy_height = jieDanBtn.cy_bottom + 45;
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                        [self.delegate calculateSelfViewHeight:jieDanBtn.cy_bottom + 45];
                    }
                }
                
            }
            
        } else {
            
            if ([driverType isEqual:@"0"]) {
                //非报价单 非货源单 独立司机
                _orderState = 1;
                
                [jieDanBtn setTitle:@"接单" forState:UIControlStateNormal];
                jieDanBtn.top = 10;
                self.cy_height = jieDanBtn.cy_bottom + 45;
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                    [self.delegate calculateSelfViewHeight:jieDanBtn.cy_bottom + 45];
                }
                
            } else {
                if ([driverType isEqual:@"2"]) {
                    //车队司机不能接单 隐藏接单区域
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                        [self.delegate calculateSelfViewHeight:0];
                    }
                    
                } else {
                    
                    //非报价单 非货源单 车队
                    _orderState = 5;
                    
                    cheShuView.hidden = NO;
                    cheShuView.top = 0;
                    [jieDanBtn setTitle:@"批量接单" forState:UIControlStateNormal];
                    
                    jieDanBtn.top = cheShuView.cy_bottom + 10;
                    self.cy_height = jieDanBtn.cy_bottom + 45;
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                        [self.delegate calculateSelfViewHeight:jieDanBtn.cy_bottom + 45];
                    }
                }
                
            }
            
        }
        
    } else {
        
        if ([isHuoYuanDan isEqual:@"0"]) {
            if ([driverType isEqual:@"0"]) {
                //报价单 货源单 独立司机
                _orderState = 2;
                
                shuLiangView.hidden = NO;
                danJiaView.hidden = NO;
                timeView.hidden = NO;
                
                danJiaView.top = shuLiangView.cy_bottom;
                timeView.top = danJiaView.cy_bottom;
                
                [jieDanBtn setTitle:@"报价承运" forState:UIControlStateNormal];
                jieDanBtn.top = timeView.cy_bottom + 10;
                self.cy_height = jieDanBtn.cy_bottom + 45;
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                    [self.delegate calculateSelfViewHeight:jieDanBtn.cy_bottom + 45];
                }
                
            } else {
                
                if ([driverType isEqual:@"2"]) {
                    //车队司机不能接单 隐藏接单区域
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                        [self.delegate calculateSelfViewHeight:0];
                    }
                    
                } else {
                    
                    //报价单 货源单 车队
                    _orderState = 6;
                    
                    shuLiangView.hidden = NO;
                    cheShuView.hidden = NO;
                    danJiaView.hidden = NO;
                    timeView.hidden = NO;
                    
                    [jieDanBtn setTitle:@"批量报价" forState:UIControlStateNormal];
                    
                    jieDanBtn.top = timeView.cy_bottom + 10;
                    self.cy_height = jieDanBtn.cy_bottom + 45;
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                        [self.delegate calculateSelfViewHeight:jieDanBtn.cy_bottom + 45];
                    }
                }
                
            }
            
        } else {
            
            if ([driverType isEqual:@"0"]) {
                //报价单 非货源单 独立司机
                _orderState = 3;
                
                danJiaView.hidden = NO;
                timeView.hidden = NO;
                
                [jieDanBtn setTitle:@"报价承运" forState:UIControlStateNormal];
                danJiaView.top = 0;
                timeView.top = danJiaView.cy_bottom;
                
                jieDanBtn.top = timeView.cy_bottom + 10;
                self.cy_height = jieDanBtn.cy_bottom + 45;
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                    [self.delegate calculateSelfViewHeight:jieDanBtn.cy_bottom + 45];
                }
                
            } else {
                
                if ([driverType isEqual:@"2"]) {
                    //车队司机不能接单 隐藏接单区域
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                        [self.delegate calculateSelfViewHeight:0];
                    }
                    
                } else {
                    
                    //报价单 非货源单 车队
                    _orderState = 7;
                    
                    cheShuView.hidden = NO;
                    danJiaView.hidden = NO;
                    timeView.hidden = NO;
                    
                    cheShuView.top = 0;
                    danJiaView.top = cheShuView.cy_bottom;
                    timeView.top = danJiaView.cy_bottom;
                    
                    [jieDanBtn setTitle:@"批量报价" forState:UIControlStateNormal];
                    
                    jieDanBtn.top = timeView.cy_bottom + 10;
                    self.cy_height = jieDanBtn.cy_bottom + 45;
                    
                    if (self.delegate && [self.delegate respondsToSelector:@selector(calculateSelfViewHeight:)]) {
                        [self.delegate calculateSelfViewHeight:jieDanBtn.cy_bottom + 45];
                    }
                }
                
            }
            
        }
        
    }
    
    xieYiButton.top = jieDanBtn.bottom + 10;
    agreeButton.top = xieYiButton.top + 5;
}

@end
